﻿#include "upromptdialog.h"
#include "uapplication.h"
#include "widgets/ulabel.h"
#include "widgets/upushbutton.h"
#include "widgets/ucommonbutton.h"

#include <QIcon>
#include <QLabel>
#include <QVBoxLayout>

class UPromptDialogPrivate
{
public:
    UPromptDialogPrivate(UPromptDialog* q) : q_ptr(q) {}
    ~UPromptDialogPrivate();

    void init(const UPromptDialog::PromptType &type);

private:
    Q_DECLARE_PUBLIC(UPromptDialog)
    Q_DISABLE_COPY(UPromptDialogPrivate)
    UPromptDialog* const q_ptr = nullptr;

    ULabel* iconLabel = nullptr;
    QLabel* describeLabel = nullptr;
    UPushButton *pCancleBtn = nullptr;
    UCommonButton *pOkBtn = nullptr;
};

UPromptDialogPrivate::~UPromptDialogPrivate()
{
}

void UPromptDialogPrivate::init(const UPromptDialog::PromptType &type)
{
    Q_Q(UPromptDialog);
    QWidget* widget = new QWidget(q);

    iconLabel = new ULabel(q);
    iconLabel->setObjectName("PromptDialog_icon");
    QIcon icon;
    switch (type) {
    case UPromptDialog::PromptType::Prompt_Info:
        icon = QIcon::fromTheme("info");
        break;
    case UPromptDialog::PromptType::Prompt_Waining:
        icon = QIcon::fromTheme("warning");
        break;
    case UPromptDialog::PromptType::Prompt_Error:
        icon = QIcon::fromTheme("error");
        break;
    case UPromptDialog::PromptType::Prompt_Fatal:
        icon = QIcon::fromTheme("fatal");
        break;
    default:
        break;
    }
    iconLabel->setIcon(icon);
    iconLabel->setFixedSize(56, 56);

    QFont font = q->font();
    font.setPixelSize(14);
    font.setWeight(QFont::Bold);
    describeLabel = new QLabel(q);
    describeLabel->setObjectName("PromptDialog_describe");
    describeLabel->setFont(font);

    QHBoxLayout* layout = new QHBoxLayout;
    layout->addSpacing(20);
    layout->addWidget(iconLabel);
    layout->addWidget(describeLabel);

    pCancleBtn = new UPushButton(QObject::tr("Cancle"), q);
    pCancleBtn->setMinimumHeight(32);
    QObject::connect(pCancleBtn, &UPushButton::clicked, q, &UPromptDialog::reject);
    pOkBtn = new UCommonButton(QObject::tr("Ok"), q);
    pOkBtn->setMinimumHeight(32);
    pOkBtn->setRadius(8);
    pOkBtn->setBackgroundRole(QPalette::Highlight);
    QObject::connect(pOkBtn, &UCommonButton::clicked, q, &UPromptDialog::accept);
    QHBoxLayout* pBtnLayout = new QHBoxLayout;
    pBtnLayout->addWidget(pCancleBtn);
    pBtnLayout->addWidget(pOkBtn);

    QVBoxLayout *pMainLayout = new QVBoxLayout;
    pMainLayout->addLayout(layout);
    pMainLayout->addSpacing(20);
    pMainLayout->addLayout(pBtnLayout);
    widget->setLayout(pMainLayout);

    q->setContentWidget(widget);
}

UPromptDialog::UPromptDialog(const PromptType &type, QWidget* parent) : UDialog(parent)
    , d_ptr(new UPromptDialogPrivate(this))
{
    Q_D(UPromptDialog);
    d->init(type);
    setTitleIcon(uApp->windowIcon());
}

UPromptDialog::~UPromptDialog()
{
}

void UPromptDialog::setDescribe(const QString &describe)
{
    Q_D(UPromptDialog);

    d->describeLabel->setText(describe);
}

QString UPromptDialog::describe()
{
    Q_D(UPromptDialog);

    return d->describeLabel->text();
}
